/**
 * 订单页js
 */
angular.module('ddapp.order', [
])

.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
	$stateProvider
	// 订单页
	.state('model.order', {
		url : '/order',
		templateUrl : 'app/order/order.html',
		controller : 'OrderController'
	})
	// 历史订单页
	.state('model.hisorder', {
		url : '/hisorder',
		templateUrl : 'app/order/hisorder.html',
		controller : 'HisorderController'
	})
}])

// 订单控制器
.controller('OrderController', ['$rootScope', '$scope', '$state', 'ShoppingCartService', 'OrderService', function($rootScope,$scope,$state, ShoppingCartService, OrderService) {
	$scope.hisorders_count = OrderService.hisorder_count();
	// 获取购物车内容
	$scope.cart = ShoppingCartService.getcart();
	$rootScope.totalprice = ShoppingCartService.gettotalprice();
	// 加入购物车
	$scope.addtocart = function(cid, did) {
		//console.log("addtocart");
		ShoppingCartService.addgoods(cid, did);
		$rootScope.buycount = ShoppingCartService.getcount();
		$rootScope.totalprice = ShoppingCartService.gettotalprice();
		// 获取购物车内容
		$scope.cart = ShoppingCartService.getcart();
	}
	// 从购物车移除
	$scope.removefromcart = function(cid, did) {
		//console.log("removefromcart");
		ShoppingCartService.removegoods(cid, did);
		$rootScope.buycount = ShoppingCartService.getcount();
		$rootScope.totalprice = ShoppingCartService.gettotalprice();
		// 获取购物车内容
		$scope.cart = ShoppingCartService.getcart();
	}
	// 判断购物车是否空
	$scope.isnotcartempty = function() {
		//console.log($scope.cart.length);
		return $scope.cart.length > 0;
	}
	// 提交订单
	$scope.submitorder = function() {
		OrderService.neworder($scope.rid, $scope.dtid, 
			{
				shopName: $scope.rname,
				tableName: $scope.dtname,
				allDishes: ShoppingCartService.getalldishes()
			},
			function(data) {
				ShoppingCartService.cleargoods();
				$rootScope.buycount = ShoppingCartService.getcount();
				$scope.hisorders_count = OrderService.hisorder_count();
				//
				$state.go('model.hisorder');
				//// 获取购物车内容
				//$scope.cart = ShoppingCartService.getcart();
				//$rootScope.totalprice = ShoppingCartService.gettotalprice();
			}
		);
	}
}])

// 订单控制器
.controller('HisorderController', ['$rootScope', '$scope', 'ShoppingCartService', 'OrderService', function($rootScope, $scope, ShoppingCartService, OrderService) {
	//$scope.rid = CONFIG.rid;
	//$scope.rname = CONFIG.rname;
	//$scope.dtid = CONFIG.dtid;
	//$scope.dtname = CONFIG.dtname;
	//$rootScope.buycount = ShoppingCartService.getcount();
	$scope.hisorders = OrderService.hisorders();
	$scope.hisorders_count = OrderService.hisorder_count();
	// 判断购物车是否空
	$scope.clearhis = function() {
		OrderService.clearhis();
		//
		$scope.hisorders = OrderService.hisorders();
		$scope.hisorders_count = OrderService.hisorder_count();
	}
}])

;